package com.franklin.validation.core.utils;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/2 15:02
 */
public abstract class DataCheckUtil {

    /**
     * 非法字符串
     */
    private static Set<String> errorStrings;

    static {
        errorStrings = new HashSet<>();
        errorStrings.add("''");
        errorStrings.add("\"\"");
        errorStrings.add("null");
        errorStrings.add("");
    }

    public static boolean isEmpty(Object obj) {
        if (null == obj) {
            return true;
        }
        if ((obj instanceof String)) {
            String trim = ((String) obj).trim();
            return errorStrings.contains(trim);
        }
        if ((obj instanceof Collection)) {
            return ((Collection) obj).size() == 0;
        }
        if ((obj instanceof Map)) {
            return ((Map) obj).size() == 0;
        }
        return false;
    }

    public static String trim(Object str) {
        return isEmpty(str) ? "" : str.toString().trim();
    }

    public static String trimNumber(Object str) {
        return isEmpty(str) ? "0" : str.toString().trim();
    }

    public static boolean isNotEmpty(Object obj){
        return !isEmpty(obj);
    }

    public static boolean checkDate(String s, String s1) {
        if (s.length() != s1.length()){
            return false;
        }
        SimpleDateFormat simpledateformat = new SimpleDateFormat(s1);
        try {
            simpledateformat.parse(s);
            return true;
        } catch (Exception ex) {

        }
        return false;
    }
}
